home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / DB_CLIPP / 0643A.ZIP / INKEY.TXT < prev    next >
Text File  |  1987-04-14  |  8KB  |  219 lines

  1. Discussion
  2.  
  3. Replacing INKEY() with INKEY.BIN
  4.  
  5. By KENNETH N. GETZ AND KAREN ROBINSON
  6.  
  7. As you may be aware, in dBASE III PLUS version 1.1, the INKEY() function does
  8. not trap Leftarrow.  For this reason, Inkey.BIN was written to replace INKEY()
  9. in those instances where you want to trap the Leftarrow key.  Inkey.BIN is an
  10. assembly language routine that checks the keyboard buffer to see if a key is
  11. pressed.  If so, it reads the key from the buffer and translates it to a code
  12. you can as you would the code returned by INKEY().  There are, however, some
  13. differences between Inkey.BIN and INKEY().  The following list summarizes the
  14. similarities and the differences.
  15.  
  16. 1. It supports the same keys INKEY() does in addition to the Leftarrow key.
  17.  
  18. 2. Inkey.BIN operates similarly to INKEY() when INKEY() is used with SET
  19.    ESCAPE OFF.  Even if you SET ESCAPE ON, pressing Esc returns CHR(27) and
  20.    does not terminate execution of your program.
  21.  
  22. 3. Inkey.BIN returns the ASCII character corresponding to the correct INKEY()
  23.    value instead of returning the actual numeric value.  This is done since it
  24.    is difficult to pass a numeric value to and from a LOADed module.  When you
  25.    use Inkey.BIN, for example, it returns a CHR(23) when you press Ctrl-W.  In
  26.    contrast, INKEY() returns a value of 23 for the same key press.  To retain
  27.    compatibility with INKEY(), use the ASC() function to convert the character
  28.    value that Inkey.BIN returns to a numeric value.
  29.  
  30. 4. INKEY() returns the values -1 through -9 for F2 through F10.  Since
  31.    Inkey.BIN returns characters instead of values, it adds 256 to the expected
  32.    return value for function keys F2 through F10, as shown in the table
  33.    below: 
  34.  
  35.      Key        INKEY()    Inkey.BIN
  36.       F1           28        28
  37.       F2           -1       255
  38.       F3           -2       254
  39.       F4           -3       253
  40.       F5           -4       252
  41.       F6           -5       251
  42.       F7           -6       250
  43.       F8           -7       249
  44.       F9           -8       248
  45.       F10       -9       247 
  46.  
  47.    In view of this difference, we suggest that you create memory variables for
  48.    the function keys F2 through F10, using the Inkey.BIN values shown above. 
  49.    Then refer to them by name instead of by their numeric values.  This makes
  50.    them easier to modify should you need to change these values.  For example,
  51.    
  52.      F1 =  28
  53.      F2 = 255
  54.      F3 = 254   
  55.             .
  56.             .
  57.             .
  58.  
  59. 5. Inkey.BIN waits in a long loop (65535 executions) for you to press a key. 
  60.    Without this loop key presses may be missed, requiring you to press the key
  61.    several times before it registers.  With the loop, Inkey.BIN never misses
  62.    any key presses, but this does present a limitation.  If no key is pressed,
  63.    Inkey.BIN waits for one to six seconds (on an IBM XT, shorter on an AT)
  64.    before it returns to dBASE III PLUS.  This means:
  65.  
  66.    -  Inkey.BIN is fine for applications where the program is to stop and wait
  67.       for a keystroke, process it, and then go on from there.
  68.    
  69.    -  The INKEY() function is better for applications like Cbmenu.PRG on the
  70.       dBASE III PLUS Samples disk, where it is used to test for a pressed key,
  71.       updating the clock if no key is pressed.  With Inkey.BIN, the clock is
  72.       updated only about once every six seconds.
  73.  
  74.  
  75. Setup 
  76.  
  77. Setup
  78.  
  79. To setup Inkey.BIN create the text file, Inkey.ASM, using MODIFY COMMAND or
  80. any text editor that writes to standard ASCII text files.
  81.  
  82. Then, assemble Inkey.ASM to a .BIN file.  Before you begin, be sure that the
  83. following files are present, either in the same subdirectory as Inkey.ASM or
  84. through the DOS PATH command: MASM.EXE, LINK.EXE, and EXE2BIN.EXE.  Follow the
  85. steps below to assemble Inkey.ASM as a .BIN file:  
  86.  
  87. 1. From the DOS prompt enter, 
  88.  
  89.        MASM Inkey.ASM;    
  90.    
  91.    This creates an object file (INKEY.OBJ).
  92.  
  93. 2. Enter, 
  94.         
  95.        LINK INKEY;
  96.    
  97.    This creates INKEY.EXE.
  98.  
  99. 3. To create a .BIN file enter,
  100.  
  101.        EXE2BIN INKEY
  102.  
  103. 4. Last, delete INKEY.EXE,
  104.  
  105.        ERASE INKEY.EXE
  106.  
  107.  
  108. Usage
  109.  
  110. Usage
  111.  
  112. To use Inkey.BIN, first LOAD it into memory.  Then initialize a return
  113. variable to a value never used by Inkey.BIN.  The example below uses CHR(200),
  114. although you may use any number between 128 and 246.  You cannot, however, use
  115. CHR(0) to test if there is a key in the keyboard buffer since dBASE III PLUS
  116. cannot pass this value as the argument of CALL command.  
  117.  
  118. Finally, CALL it from within a DO WHILE keytrap loop.  As stated before, if
  119. you want to evaluate the returned value using the values returned by INKEY(),
  120. use the ASC() function to convert the character to its numeric equivalent.
  121.  
  122. The following programs show the programming differences between INKEY() and
  123. Inkey.BIN:
  124.  
  125. With INKEY()                         With Inkey.BIN
  126. ------------                        --------------    
  127.  
  128. SET ESCAPE OFF                        LOAD Inkey
  129. SET TALK OFF                        SET TALK OFF
  130. x = 0                                    x = CHR(200)
  131. DO WHILE x = 0                        DO WHILE x = CHR(200)
  132.   x = INKEY()                              CALL Inkey WITH x
  133. ENDDO                                    ENDDO
  134. ? "INKEY(): " + STR(x,3,0)       ? "Inkey.BIN: " + STR(ASC(x),3,0)
  135.  
  136.  
  137. Program
  138.  
  139. ; Program ...: Inkey.ASM
  140. ; Author ....: Kenneth N. Getz
  141. ; Date ......: April 1, 1987
  142. ; .BIN file to be loaded from within dBASE III PLUS to replace the
  143. ; INKEY() function, since Version 1.1 doesn't trap left arrow correctly.
  144. ; The only differences are in the calling sequence (x = CHR(200),
  145. ; DO WHILE x = CHR(200)), in the return values (INKEY() returns an
  146. ; integer, Inkey.BIN returns a character), and in the use of function keys:
  147. ;
  148. ;         KEY    INKEY()        INKEY.BIN
  149. ;         ---     -------        ---------
  150. ;         F1    28        28
  151. ;         F2    -1        255
  152. ;         F3    -2        254
  153. ;         F4    -3        253
  154. ;         F5     -4        252
  155. ;         F6    -5        251
  156. ;         F7     -6        250
  157. ;         F8    -7        249
  158. ;         F9    -8        248
  159. ;         F10     -9        247
  160. ;
  161. CODE    SEGMENT    BYTE PUBLIC    'CODE'    
  162.  
  163. INKEY    PROC    FAR
  164.     ASSUME    CS:code
  165.  
  166. START:    JMP    ENTRY
  167.  
  168. DELAY    DW    0FFFFH
  169. TABLE    DB      0,  0,  0,  0,  0,  0,  0,  0,  0,  0
  170.     DB        0,  0,  0,  0, 15, 16, 17, 18, 19, 20
  171.     DB      21, 22, 23, 24, 25,  0,  0,  0,  0, 30
  172.     DB      31, 32, 33, 34, 35, 36, 37, 38,  0,  0
  173.     DB        0,  0,  0, 44, 45, 46, 47, 48, 49, 50
  174.     DB        0,  0,  0,  0,  0,  0,  0,  0, 28,255
  175.     DB    254,253,252,251,250,249,248,247,  0,  0
  176.     DB      1,  5, 18,  0, 19,  0,  4,  0,  6, 24
  177.     DB      3, 22,  7, 84, 85, 86, 87, 88, 89, 90
  178.     DB     91, 92, 93, 94, 95, 96, 97, 98, 99,100
  179.     DB    101,102,103,104,105,106,107,108,109,110
  180.     DB    111,112,113,114, 26,  2, 23, 30, 29, 31
  181.     DB     31, 31, 31, 31, 31, 31, 31, 31, 31, 31
  182.     DB     31, 31
  183.  
  184. ENTRY:    PUSH    AX            ; Save the registers used.
  185.     PUSH    CX
  186.     MOV    CX,DELAY        ; Set up for wait loop.
  187. TOP:    MOV    AH,1            ; Set for buffer poll.
  188.     INT    16H            ; See if key in buffer.
  189.     JNZ    FOUND            ; If so, jump out.
  190.     LOOP    TOP            ; Otherwise, try again DELAY times.
  191. FOUND:    JZ    EXIT            ; Quit if all the way through the
  192. loop.
  193.     AND    AH,0            ; Otherwise, set for buffer read.
  194.     INT    16H            ; Read the key from the buffer.
  195.     CMP    AL,0            ; See if we got an extended character.
  196.     JNE    ENDIT            ; If not, we're OK.
  197.     PUSH    BX            ; Otherwise, save BX for later.
  198.     PUSH    DS            ; Save DS for dBASE III PLUS.
  199.     PUSH    CS            ; Make CS and DS same for translation.
  200.     POP    DS
  201.     MOV    AL,AH            ; Move extended value into AL.
  202.     LEA    BX,TABLE        ; Get the address of the table.
  203.     DEC    AL            ; Subtract 1 to offset at 0.
  204.     XLAT                ; Do the lookup. Answer in AL.
  205.     POP    DS            ; Reset DS.
  206.     POP    BX            ; Reset BX.
  207.                     ; Send back character to dBASE III
  208. PLUS.
  209. ENDIT:    MOV    BYTE PTR [BX],AL    
  210.                                 
  211. EXIT:    POP    CX            ; Restore used registers.
  212.     POP    AX
  213.     RET
  214. INKEY    ENDP
  215. CODE    ENDS
  216.     END    START 
  217.